package com.orange.score.activity.repository;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.orange.score.activity.common.CommonFootballActivity;
import com.orange.score.app.ScoreApplication;
import com.orange.score.model.league.League;
import com.orange.score.model.league.LeagueManager;
import com.orange.score.model.repository.CupMatchType;
import com.orange.score.model.repository.RepositoryManager;
import com.orange.score.network.ResultCodeType;
import com.orange.score.service.RepositoryService;
import com.orange.score.service.RepositoryServiceCallBack;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class CupScheduleActivity extends CommonFootballActivity
  implements RepositoryServiceCallBack
{
  private static String CUP_REPOSITORY_HTML;
  public static final int MAX_LOADING_TIME = 8000;
  private static String TAG = CupScheduleActivity.class.getName();
  Button btn_cup_group_points;
  Button btn_cup_match_type;
  Button btn_cup_schedule_result;
  Button btn_cup_season;
  Bundle bundle;
  CharSequence[] cupMatchTypeString;
  List<CupMatchType> cupMatchTypelist;
  String currentCupMatchTypeId;
  String currentSeason;
  Handler errorHandler;
  Handler handler;
  boolean hasProgress;
  Timer hideDialogTimer;
  String jsUrl;
  int lastCheckedSeason = 0;
  int lastCheckedType = 0;
  League league;
  String leagueId;
  LeagueManager leagueManager;
  boolean loadingHtml;
  boolean loadingJs;
  boolean manual_load;
  RepositoryManager repositoryManager;
  CharSequence[] seasons;
  long startLoadingTime;
  TextView txt_cup_schedule_result;
  TextView txt_league_season;
  WebView webView;

  static
  {
    CUP_REPOSITORY_HTML = "file:///android_asset/cupRepository.html";
  }

  public CupScheduleActivity()
  {
    this.currentCupMatchTypeId = "";
    this.currentSeason = "";
    this.jsUrl = "";
    this.manual_load = false;
    this.loadingHtml = true;
    this.loadingJs = false;
    this.startLoadingTime = 0L;
    this.errorHandler = new Handler();
    this.hasProgress = false;
    this.handler = new Handler();
  }

  private String getJsUrl(String paramString, String[] paramArrayOfString)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("javascript:");
    localStringBuilder.append(paramString);
    localStringBuilder.append("(");
    int i = 0;
    while (true)
    {
      if (i >= paramArrayOfString.length)
      {
        localStringBuilder.append(")");
        Log.i(TAG, "loadUrl: " + localStringBuilder.toString());
        return localStringBuilder.toString();
      }
      localStringBuilder.append(paramArrayOfString[i]);
      if (i != paramArrayOfString.length - 1)
        localStringBuilder.append(",");
      ++i;
    }
  }

  private LeagueManager getLeagueManager()
  {
    return getRepositoryService().getLeagueManager();
  }

  private RepositoryManager getRepositoryManager()
  {
    return getRepositoryService().getRepositoryManager();
  }

  private RepositoryService getRepositoryService()
  {
    return ((ScoreApplication)getApplication()).getRepositoryService();
  }

  private void showDefaultWebview()
  {
    this.webView.addJavascriptInterface(new Object(this)
    {
      public void showWebview()
      {
        this.this$0.handler.post(new Runnable(this)
        {
          public void run()
          {
            CupScheduleActivity.access$4(CupScheduleActivity.5.access$0(this.this$1));
            CupScheduleActivity.5.access$0(this.this$1).loadingHtml = false;
          }
        });
      }
    }
    , "cupRepository");
  }

  private void showProgressDialog()
  {
    this.startLoadingTime = System.currentTimeMillis();
    showProgressDialog("", "加载数据中...");
    startHideDialogTimer();
    this.hasProgress = true;
  }

  private void startHideDialogTimer()
  {
    this.hideDialogTimer = new Timer();
    this.hideDialogTimer.schedule(new HideProgressTask(this, null), 1000L, 1000L);
  }

  private void stopHideDialogTimer()
  {
    if (this.hideDialogTimer != null)
    {
      this.hideDialogTimer.cancel();
      this.hideDialogTimer = null;
    }
  }

  private void updateCupMatchType()
  {
    int i;
    this.cupMatchTypelist = this.repositoryManager.getCupMatchTypeList();
    Iterator localIterator = this.cupMatchTypelist.iterator();
    if (!(localIterator.hasNext()))
    {
      this.cupMatchTypeString = new CharSequence[this.cupMatchTypelist.size()];
      i = 0;
    }
    while (true)
    {
      while (true)
      {
        CupMatchType localCupMatchType;
        do
        {
          if (i < this.cupMatchTypeString.length)
            break label103;
          return;
          localCupMatchType = (CupMatchType)localIterator.next();
        }
        while (!(localCupMatchType.getIsCurrentType().equalsIgnoreCase("TRUE")));
        this.btn_cup_match_type.setText(localCupMatchType.getMatchTypeName());
        this.currentCupMatchTypeId = localCupMatchType.getMatchTypeId();
      }
      label103: this.cupMatchTypeString[i] = ((CupMatchType)this.cupMatchTypelist.get(i)).getMatchTypeName();
      ++i;
    }
  }

  private void updateGroupPoints()
  {
    this.loadingJs = true;
    String[] arrayOfString = new String[5];
    arrayOfString[0] = "true";
    arrayOfString[1] = "'" + this.league.getLeagueId() + "'";
    arrayOfString[2] = "'" + this.currentSeason + "'";
    arrayOfString[3] = "'" + this.currentCupMatchTypeId + "'";
    arrayOfString[4] = "0";
    this.jsUrl = getJsUrl("displayCupGroupPoints", arrayOfString);
    if (!(this.loadingHtml))
      showProgressDialog();
    this.webView.loadUrl(this.jsUrl);
  }

  private void updateGroupResult()
  {
    this.loadingJs = true;
    String[] arrayOfString = new String[5];
    arrayOfString[0] = "true";
    arrayOfString[1] = "'" + this.league.getLeagueId() + "'";
    arrayOfString[2] = "'" + this.currentSeason + "'";
    arrayOfString[3] = "'" + this.currentCupMatchTypeId + "'";
    arrayOfString[4] = "0";
    this.jsUrl = getJsUrl("displayCupGroupResult", arrayOfString);
    if (!(this.loadingHtml))
      showProgressDialog();
    this.webView.loadUrl(this.jsUrl);
  }

  private void updateScheduleResult()
  {
    this.loadingJs = true;
    String[] arrayOfString = new String[5];
    arrayOfString[0] = "true";
    arrayOfString[1] = "'" + this.league.getLeagueId() + "'";
    arrayOfString[2] = "'" + this.currentSeason + "'";
    arrayOfString[3] = "'" + this.currentCupMatchTypeId + "'";
    arrayOfString[4] = "0";
    this.jsUrl = getJsUrl("displayCupScheduleResult", arrayOfString);
    if (!(this.loadingHtml))
      showProgressDialog();
    this.webView.loadUrl(this.jsUrl);
  }

  private void updateTitle()
  {
    this.txt_league_season.setText(this.league.getShortName() + "  " + this.currentSeason + "赛季");
  }

  private void updateWebview()
  {
    if (((String)this.btn_cup_match_type.getText()).indexOf("组赛") != -1)
    {
      this.btn_cup_group_points.setVisibility(0);
      this.btn_cup_schedule_result.setVisibility(0);
      this.txt_cup_schedule_result.setVisibility(8);
      this.btn_cup_group_points.setSelected(true);
      updateGroupPoints();
    }
    while (true)
    {
      return;
      this.btn_cup_group_points.setVisibility(8);
      this.btn_cup_schedule_result.setVisibility(8);
      this.txt_cup_schedule_result.setVisibility(0);
      updateScheduleResult();
    }
  }

  public void loadAllRepositoryFinish(ResultCodeType paramResultCodeType)
  {
  }

  public void loadGroupInfoFinish(ResultCodeType paramResultCodeType)
  {
    updateTitle();
    updateCupMatchType();
    if (this.manual_load)
      updateWebview();
  }

  public void loadRoundInfoFinish(ResultCodeType paramResultCodeType)
  {
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    setContentView(2130903095);
    this.bundle = getIntent().getExtras();
    this.leagueId = this.bundle.getString("leagueId");
    this.repositoryManager = getRepositoryManager();
    this.leagueManager = getLeagueManager();
    this.txt_league_season = ((TextView)findViewById(2131558619));
    this.txt_cup_schedule_result = ((TextView)findViewById(2131558625));
    this.btn_cup_match_type = ((Button)findViewById(2131558622));
    this.btn_cup_group_points = ((Button)findViewById(2131558623));
    this.btn_cup_schedule_result = ((Button)findViewById(2131558624));
    this.btn_cup_season = ((Button)findViewById(2131558620));
    this.txt_cup_schedule_result.setVisibility(8);
    this.btn_cup_group_points.setVisibility(8);
    this.btn_cup_schedule_result.setVisibility(8);
    this.hideDialogTimer = new Timer();
    this.webView = ((WebView)findViewById(2131558626));
    this.webView.getSettings().setJavaScriptEnabled(true);
    this.webView.getSettings().setBuiltInZoomControls(true);
    this.webView.setVerticalScrollBarEnabled(true);
    this.webView.setVerticalScrollbarOverlay(true);
    this.webView.getSettings().setAppCacheEnabled(true);
    this.webView.getSettings().setCacheMode(-1);
    showDefaultWebview();
    if ((this.leagueId != null) && (this.leagueId.length() > 0))
    {
      this.league = this.leagueManager.findLeagueById(this.leagueId);
      if (this.league == null)
        return;
    }
    List localList = this.league.getSeasonList();
    this.seasons = new CharSequence[localList.size()];
    int i = 0;
    while (true)
    {
      while (i >= localList.size())
      {
        this.currentSeason = ((String)this.seasons[0]);
        updateTitle();
        getRepositoryService().loadGroupInfo(this, this, this.leagueId, this.currentSeason);
        this.btn_cup_season.setOnClickListener(new View.OnClickListener(this)
        {
          public void onClick()
          {
            new AlertDialog.Builder(this.this$0).setTitle(2131361825).setSingleChoiceItems(this.this$0.seasons, this.this$0.lastCheckedSeason, new DialogInterface.OnClickListener(this)
            {
              public void onClick(, int paramInt)
              {
                Log.d(CupScheduleActivity.access$1(), "click " + paramInt);
                paramDialogInterface.dismiss();
                CupScheduleActivity.1.access$0(this.this$1).lastCheckedSeason = paramInt;
                CupScheduleActivity.1.access$0(this.this$1).currentSeason = ((String)CupScheduleActivity.1.access$0(this.this$1).seasons[paramInt]);
                Log.d(CupScheduleActivity.access$1(), "click " + CupScheduleActivity.1.access$0(this.this$1).currentSeason);
                CupScheduleActivity.access$2(CupScheduleActivity.1.access$0(this.this$1));
                CupScheduleActivity.1.access$0(this.this$1).manual_load = true;
                CupScheduleActivity.access$3(CupScheduleActivity.1.access$0(this.this$1)).loadGroupInfo(CupScheduleActivity.1.access$0(this.this$1), CupScheduleActivity.1.access$0(this.this$1), CupScheduleActivity.1.access$0(this.this$1).leagueId, CupScheduleActivity.1.access$0(this.this$1).currentSeason);
              }
            }).setNegativeButton(2131361823, new DialogInterface.OnClickListener(this)
            {
              public void onClick(, int paramInt)
              {
                Log.d(CupScheduleActivity.access$1(), "click cancel");
              }
            }).create().show();
          }
        });
        this.btn_cup_match_type.setOnClickListener(new View.OnClickListener(this)
        {
          public void onClick()
          {
            new AlertDialog.Builder(this.this$0).setTitle(2131361824).setSingleChoiceItems(this.this$0.cupMatchTypeString, this.this$0.lastCheckedType, new DialogInterface.OnClickListener(this)
            {
              public void onClick(, int paramInt)
              {
                Log.d(CupScheduleActivity.access$1(), "click " + paramInt);
                paramDialogInterface.dismiss();
                CupScheduleActivity.2.access$0(this.this$1).lastCheckedType = paramInt;
                String str = (String)CupScheduleActivity.2.access$0(this.this$1).cupMatchTypeString[paramInt];
                Log.d(CupScheduleActivity.access$1(), "click " + str);
                CupScheduleActivity.2.access$0(this.this$1).btn_cup_match_type.setText(str);
                CupScheduleActivity.2.access$0(this.this$1).currentCupMatchTypeId = ((CupMatchType)CupScheduleActivity.2.access$0(this.this$1).cupMatchTypelist.get(paramInt)).getMatchTypeId();
                CupScheduleActivity.access$4(CupScheduleActivity.2.access$0(this.this$1));
              }
            }).setNegativeButton(2131361823, new DialogInterface.OnClickListener(this)
            {
              public void onClick(, int paramInt)
              {
                Log.d(CupScheduleActivity.access$1(), "click cancel");
              }
            }).create().show();
          }
        });
        this.btn_cup_group_points.setOnClickListener(new View.OnClickListener(this)
        {
          public void onClick()
          {
            this.this$0.btn_cup_group_points.setSelected(true);
            this.this$0.btn_cup_schedule_result.setSelected(false);
            CupScheduleActivity.access$5(this.this$0);
          }
        });
        this.btn_cup_schedule_result.setOnClickListener(new View.OnClickListener(this)
        {
          public void onClick()
          {
            this.this$0.btn_cup_group_points.setSelected(false);
            this.this$0.btn_cup_schedule_result.setSelected(true);
            if (((String)this.this$0.btn_cup_match_type.getText()).indexOf("组赛") != -1)
              CupScheduleActivity.access$6(this.this$0);
            while (true)
            {
              this.this$0.btn_cup_group_points.setSelected(false);
              this.this$0.btn_cup_schedule_result.setSelected(true);
              return;
              CupScheduleActivity.access$7(this.this$0);
            }
          }
        });
      }
      this.seasons[i] = ((CharSequence)localList.get(i));
      ++i;
    }
  }

  protected void onPause()
  {
    super.onPause();
    hideDialog();
    this.hasProgress = false;
    this.loadingJs = false;
    stopHideDialogTimer();
  }

  public void onRefresh()
  {
    this.btn_cup_group_points.setSelected(false);
    this.btn_cup_schedule_result.setSelected(false);
    this.loadingHtml = true;
    showProgressDialog();
    this.webView.loadUrl(CUP_REPOSITORY_HTML);
  }

  protected void onResume()
  {
    showProgressDialog();
    this.webView.loadUrl(CUP_REPOSITORY_HTML);
    this.manual_load = false;
    this.loadingHtml = true;
    super.onResume();
  }

  private class HideProgressTask extends TimerTask
  {
    public void run()
    {
      if ((this.this$0.webView.getProgress() == 100) && (this.this$0.hasProgress) && (((this.this$0.loadingHtml) || (this.this$0.loadingJs))))
      {
        this.this$0.hideDialog();
        CupScheduleActivity.access$0(this.this$0);
        this.this$0.hasProgress = false;
        this.this$0.loadingJs = false;
      }
      while (true)
      {
        do
          return;
        while (System.currentTimeMillis() - this.this$0.startLoadingTime <= 8000L);
        this.this$0.hideDialog();
        CupScheduleActivity.access$0(this.this$0);
        this.this$0.webView.stopLoading();
        this.this$0.hasProgress = false;
        this.this$0.loadingJs = false;
        Log.w(CupScheduleActivity.access$1(), "network speed too slow!");
        this.this$0.errorHandler.post(new Runnable(this)
        {
          public void run()
          {
            Toast.makeText(CupScheduleActivity.HideProgressTask.access$1(this.this$1), "你的网速太慢啦，加载失败，请刷新重新加载！", 0).show();
          }
        });
      }
    }
  }
}